// pages/home/index.js
const app = getApp()
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title': '海螺号工蜂',
            'color': true,
            'class': false
        },
        pageHeight: app.globalData.pageHeight, //手机屏幕高度
        navH: app.globalData.navHeight,
        dateSelector: app.MultipleSelectDate,
        date: "自定义",
        days: 1,
        salesmanId: wx.getStorageSync('userInfo').id,
        timeRange: [],
        // dialog: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // var that = this;
        // wx.getStorage({
        //     key: 'userInfo',
        //     success(res) {
        //         that.setData({
        //             salesmanId: res.data.id,
        //         })
        //         that.income()
        //         that.incomes()
        //         // that.debt()
        //     }
        // })
    },

    /* 生命周期函数--监听页面显示 */
    onShow: function () {
        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    salesmanId: res.data.id,
                })
                that.income()
                that.incomes()
                // that.debt()
            }
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log('开始下拉刷新')
        wx.showNavigationBarLoading() //在标题栏中显示加载图标
        this.income()
        this.incomes()
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //得到数据后停止下拉刷新
        console.log('下拉刷新成功')
    },

    /* 首页收益汇总 */
    income(e) {
        var that = this,
            data = {
                salesmanId: that.data.salesmanId,
                days: that.data.days,
                timeRange: that.data.timeRange
            }
        // /WorkerBeeHeadInfo/index/
        app.basePost(app.U({
            m: 'bdservice',
            c: 'WorkerBeeHeadInfo',
            a: 'index/'
        }), data, res => {
            console.log(1)
            that.setData({
                income: res.data
            })
        }, (res) => {})
    },

    /* 首页收益汇总2 */
    incomes(e) {
        var that = this
        // /WorkerBeeHeadInfo/findRealTimeData/
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'WorkerBeeHeadInfo',
            a: 'findRealTimeData/',
            q: {
                salesmanId: that.data.salesmanId
            }
        }), (res) => {}, res => {
            that.setData({
                incomes: res.data
            })
        })
    },

    // /* 欠款提醒 */
    // debt(e) {
    //     var that = this;
    //     // /fms/accountsRechargeOrder/checkEnsureMoneyStatus
    //     app.baseGet(app.U({
    //         m: 'fms',
    //         c: 'accountsRechargeOrder',
    //         a: 'checkEnsureMoneyStatus',
    //         q: {
    //             deptId: wx.getStorageSync('userInfo').id,
    //             deptType: wx.getStorageSync('userInfo').userType
    //         }
    //     }), res => {
    //         console.log(res)
    //         that.setData({
    //             ['dialog.debt']: res.data,
    //             ['dialog.show']: res.data.ensureMoney <= res.data.oweMoney ? true : false
    //         })
    //     }, (res) => {})
    // },

    /* 点击选择时间 */
    handleSummary(e) {
        var dateIndex = e.currentTarget.dataset['index']; //日期
        this.setData({
            days: dateIndex,
            timeRange: [],
            date: '自定义'
        })
        this.income()
    },

    /* 自定义时间 */
    bindMultiPickerChange: function (e) {
        var date = e.detail.value
        var timeRange = [];
        var startTime = this.data.dateSelector[0][e.detail.value[0]] + this.data.dateSelector[1][e.detail.value[1]] + this.data.dateSelector[2][e.detail.value[2]]
        var endTime = this.data.dateSelector[3][e.detail.value[3]] + this.data.dateSelector[4][e.detail.value[4]] + this.data.dateSelector[5][e.detail.value[5]]
        timeRange.push(startTime, endTime)
        this.setData({
            multiIndex: e.detail.value,
            date: this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]],
            days: null,
            timeRange: timeRange
        })
        this.income()
    },

    //跳转到收益流水
    earnings() {
        wx.navigateTo({
            url: '/pages/subrogation/earnings/index',
        })
    },
    //跳转到铺货bar
    distribution() {
        wx.switchTab({
            url: '/pages/commodity/index',
        })
    },
    //跳转到订单与分账bar
    OrderOrledger() {
        wx.switchTab({
            url: '/pages/orders/index',
        })
    },
    //店铺欠款
    Storesarrears() {
        wx.navigateTo({
            url: '/pages/subrogation/debtInfo/index',
        })
    },
    //跳转到店铺与库存bar
    StoresOrInventory() {
        wx.switchTab({
            url: '/pages/shop/index',
        })
    },

    // goPay() {
    //     wx.navigateTo({
    //         url: '/pages/subrogation/moneyPay/index?money=' + this.data.debt.proposalRechargeMoney,
    //     })
    // },

    // exit() {
    //     wx.exitMiniProgram({
    //         success: function (res) {
    //             console.log(res)
    //         }
    //     })
    // },
})